package com.audi.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.audi.R;
import com.audi.activity.AddActionCardActivity;
import com.audi.dto.SmartTriggerItem;
import com.audi.dto.SmartTriggerWhenTriggerItem;
import com.audi.message.SendMessage;

public class SmartTriggerAdapter extends BaseAdapter {
	private Activity mActivity;
	private ArrayList<SmartTriggerItem> mArrTrigger;
	// VIEWHOLDER HOLDER;
	int posit;

	public SmartTriggerAdapter(Activity activity,
			ArrayList<SmartTriggerItem> mListTriggerArr) {
		this.mActivity = activity;
		this.mArrTrigger = mListTriggerArr;
	}

	public void swapItems(ArrayList<SmartTriggerItem> mArrTrigger) {
		this.mArrTrigger = mArrTrigger;
		notifyDataSetChanged();
	}

	private class ViewHolder {
		TextView txtDevNameWhen, txtDevNameThen, txtRoomWhen, txtRoomThen,
				txtDescription;
		ImageButton ibtnOff, ibtnOn, ibtnNext, ibtnControl;
		LinearLayout lnlRowSmartTrigger;
		Switch switchOnOff;
	}

	@Override
	public int getCount() {
		return mArrTrigger.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mArrTrigger.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		posit = position;
		LayoutInflater mInflater = (LayoutInflater) mActivity
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.row_smart_trigger, null);
			holder = new ViewHolder();
			// holder.txtWhen = (TextView)
			// convertView.findViewById(R.id.txt_when);
			holder.txtDescription = (TextView) convertView
					.findViewById(R.id.txt_description);
			holder.txtDevNameWhen = (TextView) convertView
					.findViewById(R.id.device_name_when);
			holder.txtDevNameThen = (TextView) convertView
					.findViewById(R.id.device_name_then);
			holder.txtRoomWhen = (TextView) convertView
					.findViewById(R.id.room_name_when);
			holder.txtRoomThen = (TextView) convertView
					.findViewById(R.id.room_name_then);
			holder.ibtnOff = (ImageButton) convertView
					.findViewById(R.id.ibtn_dishighlighted);
			holder.ibtnOn = (ImageButton) convertView
					.findViewById(R.id.ibtn_highlighted);
			holder.ibtnNext = (ImageButton) convertView
					.findViewById(R.id.ibtn_next);
			holder.switchOnOff = (Switch) convertView
					.findViewById(R.id.switch_on_off_trigger);

			holder.lnlRowSmartTrigger = (LinearLayout) convertView
					.findViewById(R.id.lnl_row_smart_trigger);
			holder.ibtnControl = (ImageButton) convertView
					.findViewById(R.id.ibtn_on_off);

			convertView.setTag(holder);
			// convertView.setId(position);
		} else {

			holder = (ViewHolder) convertView.getTag();

		}

		// holder.txtWhen.setText("When");

		holder.txtDescription
				.setText("If %sensor_name% is triggered, then open the %device_name% for 5 mins.");
		holder.txtDevNameWhen.setText(mArrTrigger.get(position).getWhen()
				.getWhenDeviceName());
		holder.txtDevNameThen.setText(mArrTrigger.get(position).getTrigger()
				.getTriggerDeviceName());
		holder.txtRoomThen.setText(mArrTrigger.get(position).getWhen()
				.getWhenDevicePlaceName());
		holder.txtRoomWhen.setText(mArrTrigger.get(position).getTrigger()
				.getTriggerDevicePlaceName());

		holder.ibtnOff.setImageResource(R.drawable.icon_plug_on_off);
		holder.ibtnNext.setImageResource(R.drawable.btn_next);
		holder.ibtnOn.setImageResource(R.drawable.icon_plug_on_off);
		holder.ibtnControl.setImageResource(R.drawable.btn_place_card_onoff_on);
		holder.ibtnControl.setTag(position);
		holder.switchOnOff.setId(position);
		holder.lnlRowSmartTrigger.setId(position);
		holder.switchOnOff.setOnCheckedChangeListener(null);
		if (mArrTrigger.get(position).getWhenTrigger().getOnOff() == 0) {
			holder.switchOnOff.setChecked(false);
			holder.lnlRowSmartTrigger.setAlpha(0.3f);
		} else {
			holder.switchOnOff.setChecked(true);
			holder.lnlRowSmartTrigger.setAlpha(1);
		}
		// convertView.setTag(holder);
		holder.switchOnOff
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						SmartTriggerWhenTriggerItem item = new SmartTriggerWhenTriggerItem();
						item = mArrTrigger.get(buttonView.getId())
								.getWhenTrigger();
						if (isChecked == true) {
							Log.e("dfdfdf",
									"dfdfdfdfd TRUE " + buttonView.getId());
							item.setOnOff(1);
							SendMessage message = new SendMessage();
							message.sendData(3017, item, handlerOnOffTrigger);
							// holder.lnlRowSmartTrigger.setAlpha(0.3f);
							notifyDataSetChanged();
						} else {
							// Log.e("dfdfdf", "dfdfdfdfd FALSE");
							item.setOnOff(0);
							SendMessage message = new SendMessage();
							message.sendData(3017, item, handlerOnOffTrigger);
							// holder.lnlRowSmartTrigger.setAlpha(1);
							notifyDataSetChanged();
							Log.e("dfdfdf",
									"dfdfdfdfd False " + buttonView.getId());
						}

					}
				});

		// holder.ibtnControl.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		//
		// int foo = (Integer)v.getTag();
		// DescriptionSmartTrigger rowI = (DescriptionSmartTrigger)
		// getItem(foo);
		// if(rowI.getTriggerStatus()==1){
		// holder.lnlRowSmartTrigger.setAlpha(0.3f);
		// rowI.setTriggerStatus(0);
		// holder.ibtnControl.setImageResource(R.drawable.btn_place_card_onoff_off);
		// }
		// else {
		// holder.lnlRowSmartTrigger.setAlpha(1);
		// rowI.setTriggerStatus(1);
		// holder.ibtnControl.setImageResource(R.drawable.btn_place_card_onoff_on);
		// }
		// //holder.lnlRowSmartTrigger.setBackgroundColor(R.drawable.btn_dash_blue);
		// //DescriptionSmartTrigger rowI = (DescriptionSmartTrigger)
		// getItem(posit);
		// Toast.makeText(mActivity,v.getTag().toString()
		// ,Toast.LENGTH_SHORT).show();
		// }
		// });

		holder.lnlRowSmartTrigger.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View viewItem) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mActivity,
						AddActionCardActivity.class);
				intent.putExtra("TYPE_ACTION", 1);
				intent.putExtra("TriggerItem", mArrTrigger.get(viewItem.getId()));
				mActivity.startActivity(intent);
				
			}
		});
		return convertView;
	}

	Handler handlerOnOffTrigger = new Handler() {
		public void handlemessage(final Message msg) {
			new Runnable() {
				public void run() {
					int result;
					result = msg.getData().getInt("3017");

					Log.d("LOG=========", "RUN On OFF======>>>>>>" + result);

				}
			};
		}
	};

}
